package cao.oschina.function.multiple.blog;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import org.apache.http.Header;

import java.util.List;

import cao.oschina.R;
import cao.oschina.api.URLs;
import cao.oschina.function.multiple.MultipleFunction;
import cao.oschina.bean.Blog;
import cao.oschina.ui.Main;
import cao.oschina.util.TimeUtil;
import cao.oschina.widget.PushDownListView;
import cao.oschina.widget.RefreshView;
import hd.util.adapter.BaseViewHolder;
import hd.util.adapter.UniAdapter;
import hd.util.http.HttpClient;
import hd.util.http.HttpRequestListener;
import hd.util.http.HttpResponse;
import hd.util.widget.listview.BasePushDownListView;

/**
 * 博客列表
 * <p/>
 * Created by Haidy on 15/3/28.
 */
public class BlogFunc extends MultipleFunction {

    public static final String TYPE_LATEST = "latest";
    public static final String TYPE_RECOMMEND = "recommend";

    private PushDownListView listView;
    private BlogAdapter adapter;
    private int page = 0;

    private String type;

    public BlogFunc(View view, Main main, String type) {
        super(view, main);
        this.type = type;
        loadData(true);
    }

    @Override
    protected void initView() {
        listView = (PushDownListView) view.findViewById(R.id.blog_list_view);
        adapter = new BlogAdapter(app);
        listView.setAdapter(adapter);
        listView.setOnRefreshListener(new PushDownListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (!isLoading) {
                    getAll = false;
                    page = 0;
                    loadData(true);
                }
            }
        });
        listView.setOnLoadNextPageListener(new BasePushDownListView.OnLoadNextPageListener() {
            @Override
            public void onLoad() {
                if (!getAll && !isLoading) {
                    page++;
                    loadData(false);
                }
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (adapter.getCount() > 1 && position != adapter.getCount() - 1) {
                    Intent intent = new Intent(view.getContext(), BlogDetail.class);
                    Blog blog = adapter.getItem(position - 1);
                    intent.putExtra("id", blog.id);
                    intent.putExtra("comment_count", blog.commentCount);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    view.getContext().startActivity(intent);
                    main.activityPushAnim();
                }
            }
        });
    }


    @Override
    protected void initData() {
    }

    /**
     * 加载数据
     *
     * @param refresh 是否刷新
     */
    private void loadData(final boolean refresh) {
        listView.setEnabled(false);
        isLoading = true;
        new HttpClient().httpGet(URLs.BLOG_LIST + "?pageSize=20&type=" + type + "&pageIndex=" + page, (Header[]) null, new HttpRequestListener() {
            @Override
            public void success(HttpResponse response) {
                List<Blog> newsList = Blog.parser(response.text);
                if (refresh) {
                    adapter.setData(newsList);
                } else {
                    if (adapter.getCount() != 0) {
                        adapter.remove(adapter.getCount() - 1);
                    }
                    adapter.addAllData(newsList);
                }
                adapter.addData(null);
                listView.overHeadScroll();
                listView.setEnabled(true);
            }

            @Override
            public void failure(HttpResponse response) {
                listView.overHeadScroll();
            }

            @Override
            public void end() {
                isLoading = false;
            }
        });
    }


    private class BlogAdapter extends UniAdapter<Blog, ViewHolder> {

        public BlogAdapter(Context context) {
            super(context);
        }

        @Override
        public int getContentView() {
            return R.layout.blog_item_view;
        }

        @Override
        public BaseViewHolder<Blog, ViewHolder> getHolder() {
            return new BaseViewHolder<Blog, ViewHolder>() {
                @Override
                public void loadData(ViewHolder holder, Blog data, int position) {
                    if (data != null) {
                        if (holder.itemView.getVisibility() == View.GONE) {
                            holder.itemView.setVisibility(View.VISIBLE);
                            holder.nextPageView.setVisibility(View.GONE);
                            holder.refreshView.stopRotation();
                        }
                        if (data.documentType == 0) {
                            holder.typeView.setImageResource(R.mipmap.widget_repaste_icon);
                        } else {
                            holder.typeView.setImageResource(R.mipmap.widget_original_icon);
                        }
                        holder.titleView.setText(data.title);
                        holder.contentView.setText(data.body);
                        holder.pubDataView.setText(TimeUtil.friendTime(data.pubDate));
                        holder.commentCountView.setText(String.valueOf(data.commentCount));
                    } else {
                        if (holder.nextPageView.getVisibility() == View.GONE) {
                            holder.itemView.setVisibility(View.GONE);
                            holder.nextPageView.setVisibility(View.VISIBLE);
                            holder.refreshView.startRotation();
                        }
                    }
                }
            };
        }

        @Override
        public ViewHolder initViewHolder(View convertView) {
            return new ViewHolder(convertView);
        }
    }

    private class ViewHolder {
        public final View itemView;

        public final ImageView typeView;
        public final TextView titleView;
        public final TextView contentView;
        public final TextView author;
        public final TextView pubDataView;
        public final TextView commentCountView;

        public final View nextPageView;

        public final RefreshView refreshView;

        public ViewHolder(View convertView) {
            itemView = convertView.findViewById(R.id.item_view);

            typeView = (ImageView) convertView.findViewById(R.id.type);
            titleView = (TextView) convertView.findViewById(R.id.title);
            contentView = (TextView) convertView.findViewById(R.id.content);
            author = (TextView) convertView.findViewById(R.id.author);
            pubDataView = (TextView) convertView.findViewById(R.id.pub_date);
            commentCountView = (TextView) convertView.findViewById(R.id.comment_count);

            nextPageView = convertView.findViewById(R.id.next_pager);
            refreshView = (RefreshView) convertView.findViewById(R.id.next_pager_refresh_view);
        }
    }
}
